//
//  UIView+BCError.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/3.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "UIView+BCError.h"

static NSInteger UIViewBCErrorViewTag = 100101;

@implementation UIView (BCError)


- (void)bc_showError:(nonnull NSError *)error {
    [self bc_showWithInsets:UIEdgeInsetsZero error:error needRefresh:false refreshTitle:nil target:nil refreshAction:nil];
}
- (void)bc_showError:(nonnull NSError *)error needRefresh:(BOOL)needRefresh refreshTitle:(nullable NSString *)refreshTitle target:(nullable id)target refreshAction:(nullable SEL)action {
    [self bc_showWithInsets:UIEdgeInsetsZero error:error needRefresh:needRefresh refreshTitle:refreshTitle target:target refreshAction:action];
}
- (void)bc_showWithInsets:(UIEdgeInsets)insets error:(nonnull NSError *)error needRefresh:(BOOL)needRefresh refreshTitle:(nullable NSString *)refreshTitle target:(nullable id)target refreshAction:(nullable SEL)action  {
    QMUIEmptyView *errorView = [self viewWithTag:UIViewBCErrorViewTag];
    if (!errorView) {
        errorView = [QMUIEmptyView new];
        errorView.tag = UIViewBCErrorViewTag;
        errorView.backgroundColor = [UIColor generalWhiteBackgroundColor];
        [self addSubview:errorView];
        [errorView mas_makeConstraints:^(MASConstraintMaker *make) {
            if ([self isKindOfClass:UIScrollView.class]) {
                make.top.mas_equalTo(self).offset(insets.top);
                make.left.mas_equalTo(self).offset(insets.left);
                make.width.mas_equalTo(self).offset(-insets.left-insets.right);
                make.height.mas_equalTo(self).offset(-insets.top-insets.bottom);
            }else {
                make.edges.mas_equalTo(self).insets(insets);
            }
        }];
        errorView.verticalOffset = -30.0;
        //
        [errorView.actionButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        errorView.actionButton.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 5, 8);
        errorView.actionButton.layer.cornerRadius = 2;
        errorView.actionButton.layer.borderColor = [UIColor grayColor].CGColor;
        errorView.actionButton.layer.borderWidth = .5;
        errorView.actionButtonTitleColor = [UIColor grayColor];
        //
        errorView.textLabelTextColor = [UIColor grayColor];
    }
    //
    [errorView setLoadingViewHidden:true];
//    errorView.textLabelInsets = UIEdgeInsetsZero;
    [errorView setTextLabelText:error.domain];
//    [errorView setDetailTextLabelText:error.domain];
    if (needRefresh) {
        if (refreshTitle.length) {
            [errorView setActionButtonTitle:refreshTitle];
        }else {
            [errorView setActionButtonTitle:BCLocalizedKey(@"Clicked to refresh")];
        }
    }else {
        [errorView setActionButtonTitle:nil];
    }
}

- (void)bc_hiddenError {
    QMUIEmptyView *errorView = [self viewWithTag:UIViewBCErrorViewTag];
    if (errorView) {
        [errorView removeFromSuperview];
    }
}

@end
